package com.example.widget_lib.widget.progress.horbar;

import com.example.widget_lib.utils.LogUtil;
import com.example.widget_lib.utils.PxUtil;
import com.example.widget_lib.utils.Util;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

public class ShadowProBar extends Component implements Component.DrawTask {
    /**
     * 执行中
     */
    private static final Color[] COLORS_WAING = {new Color(Color.rgb(101, 226, 175)), new Color(Color.rgb(88, 181, 250))};
    /**
     * 已完成
     */
    private static final Color[] COLORS_FINISH = {new Color(Color.rgb(255, 196, 0)), new Color(Color.rgb(255, 110, 77))};

    private Paint bgPaint;
    private Paint progressPaint;
    /**
     * 进度条弧度
     */
    private int progressRound;
    /**
     * 进度条背景颜色
     */
    private Color bgColor = Color.GRAY;
    /**
     * 进度条颜色
     */
    private Color progressColor = Color.BLUE;

    private Color[] colors = COLORS_WAING;


    private RectFloat bgRectF = new RectFloat();

    private RectFloat progressRectF = new RectFloat();
    private float mProgress;

    /**
     * 动画执行时间
     */
    private int duration = 1000;
    /**
     * 动画延时启动时间
     */
    private int startDelay = 500;

    /**
     * 进度动画
     */
    private AnimatorValue progressAnimator;

    /**
     * 当前进度
     */
    private float currentProgress;
    // 是否控制进度动画
    private boolean isCtrlAnim = true;

    public ShadowProBar(Context context) {
        super(context);
    }

    public ShadowProBar(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
        initPaint();
        addDrawTask(this);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    protected void onDetachedFromWindow() {
        if (progressAnimator != null) {
            progressAnimator.cancel();
        }
    }

    /**
     * 设置进度条状态
     *
     * @param isFinish
     */
    public void setProStatus(boolean isFinish) {
        if (isFinish) {
            this.colors = COLORS_FINISH;
        } else {
            this.colors = COLORS_WAING;
        }
    }

    public void setProgressColor(Color[] colors) {
        this.colors = colors;
    }

    /**
     * 初始化画笔宽度及view大小
     */
    private void init() {
        progressRound = (int) PxUtil.vp2px(6);
        this.colors = COLORS_WAING;
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        bgPaint = getPaint(progressRound, bgColor, Paint.Style.FILL_STYLE);
        progressPaint = getPaint(progressRound, progressColor, Paint.Style.FILL_STYLE);
    }

    private Paint getPaint(int strokeWidth, Color color, Paint.Style style) {
        Paint paint = new Paint();
        paint.setStrokeWidth(strokeWidth);
        paint.setColor(color);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setStyle(style);
        return paint;
    }

    //    @Override
//    protected void onDraw(Canvas canvas) {
//        drawBgProgress(canvas);
//        drawProgress(canvas);
//    }
    @Override
    public void onDraw(Component component, Canvas canvas) {
        onSizeChanged();
        drawBgProgress(canvas);
        drawProgress(canvas);
    }

    protected void onSizeChanged() {
        bgRectF.left = 0f;
        bgRectF.right = getWidth();
        bgRectF.top = 0;
        bgRectF.bottom = getHeight();
        progressRectF.left = 0f;
        progressRectF.top = 0f;
        progressRectF.bottom = getHeight();
    }

//    @Override
//    public void setComponentSize(int width, int height) {
//        super.setComponentSize(width, height);
//        bgRectF.left = 0f;
//        bgRectF.right = width;
//        bgRectF.top = 0;
//        bgRectF.bottom = height;
//        progressRectF.left = 0f;
//        progressRectF.top = 0f;
//        progressRectF.bottom = height;
//    }

    private void drawBgProgress(Canvas canvas) {
        canvas.drawRoundRect(bgRectF, progressRound, progressRound, bgPaint);
    }

    private void drawProgress(Canvas canvas) {
//        LinearGradient sweepGradient = new LinearGradient(0f, getHeight(), currentProgress, getHeight(), colors, null, Shader.TileMode.MIRROR_TILEMODE);
//        LinearShader shader = new LinearShader(points, null, colors, Shader.TileMode.CLAMP_TILEMODE);
        Point[] points = new Point[]{new Point(0f, getHeight()), new Point(currentProgress, getHeight())};
        LinearShader linearShader = new LinearShader(points, null, colors, Shader.TileMode.MIRROR_TILEMODE);
        LinearShader shader = linearShader;
        progressPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
        progressRectF.right = currentProgress;
        canvas.drawRoundRect(progressRectF, progressRound, progressRound, progressPaint);
    }

    /**
     * 设置进度条带动画效果
     *
     * @param progress
     * @return  ShadowProBar
     */
    public ShadowProBar setProgressWithAnimation(float progress) {
        mProgress = progress;
        if (isCtrlAnim) {
            initAnimation();
        } else {
            invalidate();
        }
        return this;
    }

    public void isCtrlAnim(boolean isAnim) {
        isCtrlAnim = isAnim;
    }

    /**
     * 进度移动动画  通过插值的方式改变移动的距离
     */
    private void initAnimation() {
//        progressAnimator = AnimatorValue.ofFloat(0, mProgress);
        progressAnimator = new AnimatorValue();
        progressAnimator.setDuration(duration);
//        progressAnimator.setStartDelay(startDelay);
        progressAnimator.setDelay(startDelay);
        progressAnimator.setCurveType(AnimatorValue.CurveType.ACCELERATE);
        progressAnimator.setValueUpdateListener((animatorValue, v) -> {

//            currentProgress = Math.min(v * getWidth(), mProgress * (v * getWidth() / 100));
            currentProgress = mProgress * (v * getWidth() / 100);

            if (currentProgress < progressRound && currentProgress != 0) {
                currentProgress = progressRound;
            }
            if (currentProgress >= getWidth()) {
                currentProgress = getWidth();
            }
            //进度回调方法
//                if (progressListener != null) {
//                    progressListener.currentProgressListener(value);
//                }
            invalidate();
        });
//        progressAnimator.setInterpolator(new LinearInterpolator());
//        progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(AnimatorValue valueAnimator) {
//                float value = (float) valueAnimator.getDelay();//getAnimatorValue();
//                //进度数值只显示整数，我们自己的需求，可以忽略
//                //把当前百分比进度转化成view宽度对应的比例
//                currentProgress = getWidth() * value / 100;
//
//                if (currentProgress < progressRound && currentProgress != 0) {
//                    currentProgress = progressRound;
//                }
//
//                if (currentProgress > getWidth()) {
//                    currentProgress = getWidth();
//                }
//                //进度回调方法
////                if (progressListener != null) {
////                    progressListener.currentProgressListener(value);
////                }
//                invalidate();
//            }
//        });
        progressAnimator.start();
    }
}
